package com.wyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.response.PageInfo;
import com.wyh.common.result.R;
import com.wyh.entity.AdminBlackList;
import com.wyh.mapper.AdminBlackListMapper;
import com.wyh.request.AdminBlackListRequest;
import com.wyh.service.IAdminBlackListService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 黑名单管理业务层处理
 *
 * @author WangYuHang
 * @date 2024-03-03
 */
@Slf4j
@Service
@AllArgsConstructor
public class AdminBlackListServiceImpl implements IAdminBlackListService {

    private final AdminBlackListMapper baseMapper;

    @Override
    public Boolean isBlack(String userId1, String userId2) {
        AdminBlackList adminBlackList = baseMapper.selectOne(Wrappers.lambdaQuery(AdminBlackList.class)
                //用户2
                .eq(AdminBlackList::getUserId, userId2)
                //用户1
                .eq(AdminBlackList::getBlackUserId,userId1));
        if (adminBlackList != null) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdminBlackList add(AdminBlackListRequest request) {
        AdminBlackList entity = AdminBlackList.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AdminBlackListRequest request) {
        AdminBlackList entity = AdminBlackList.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public AdminBlackList getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminBlackList> pageList(AdminBlackListRequest request) {
        Page<AdminBlackList> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<AdminBlackList> query = this.buildWrapper(request);
        IPage<AdminBlackList> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<AdminBlackList> list(AdminBlackListRequest request) {
        LambdaQueryWrapper<AdminBlackList> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<AdminBlackList> buildWrapper(AdminBlackListRequest param) {
        LambdaQueryWrapper<AdminBlackList> query = new LambdaQueryWrapper<>();
        if (param.getUserId() != null){
               query.eq(AdminBlackList::getUserId ,param.getUserId());
        }
        if (param.getBlackUserId() != null){
               query.eq(AdminBlackList::getBlackUserId ,param.getBlackUserId());
        }
        if (param.getIsDelete() != null){
               query.eq(AdminBlackList::getIsDelete ,param.getIsDelete());
        }
        if (param.getCreateUserId() != null){
               query.eq(AdminBlackList::getCreateUserId ,param.getCreateUserId());
        }
        if (param.getUpdateUserId() != null){
               query.eq(AdminBlackList::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
